package lupita.controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lupita.persistence.MySQLImpl;

/**
 * Servlet implementation class EntrarSvt
 */
public class EntrarSvt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private MySQLImpl mysql;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EntrarSvt() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SortedMap<Integer, String> users =  new TreeMap<Integer, String>(java.util.Collections.reverseOrder()) ;
		int userID = 0;
		String user;
		
		try {
			mysql = new MySQLImpl();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ResultSet result;
		try{
			mysql.openConnection();
			result=  mysql.selectSQL("select * from user");
		
			
			result.beforeFirst();
		
			System.out.println("TABLA USERS");
		
									
						
		
			while(result.next()) {
				userID = result.getInt("id");
				user = result.getString("username");			
			
				System.out.println(userID+" "+user+" ");
				users.put(userID,user );
			}
		
			mysql.closeConnection();
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		request.setAttribute("users", users);
		
		
		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/index.jsp");
		requestDispatcher.forward(request, response);
	}
}
